angular.module('mamApp').controller('programUploadController', ['$scope', '$state', '$uibModalInstance', 'FileUploader', 'api', 'AlbumManager','albumId', function ($scope, $state, $uibModalInstance, FileUploader, api, AlbumManager,albumId) {

    $scope.userInfo = JSON.parse(localStorage.getItem("userInfo"))
    $scope.image_a = false;
    $scope.disabled=false;
    $scope.list = [];


    $scope.removeFile=function ($index,item) {
        $scope.list.forEach(function (file) {
            if(file.file.webUrl==item.webUrl){
                $scope.list.splice($scope.list.indexOf(file),1);
            }
        })
    }
    $scope.uploaderAudio = new FileUploader({
        alias: "uploadFile",
        queueLimit: 20,//文件个数
        // removeAfterUpload: true,//上传后删除文件 */
        filters: [{
            name: 'uploadFile',//后台接收字段
            fn: function (item /*{File|FileLikeObject}*/, options) {
                if (!/\.(mp3|MP3|AAC|aac)$/.test(item.name)) {
                    $scope.imageMsg = "最多只能上传" + 20 + "条mp3或者AAC格式且每个小于" + 150 + "MB的音频";
                    $scope.image_a = true;
                    return false;
                }
                else if (item.size > 150 * 1024 * 1024||item.size < 0.5 * 1024 * 1024) {
                    $scope.imageMsg = "最多只能上传" + 20 + "条mp3或者AAC格式且每个小于" + 150 + "MB的音频";
                    $scope.image_a = true;
                    return false;
                } else {
                    $scope.image_a = false;
                    return true;
                }

            }
        }],
        onSuccessItem: function (fileItem, response, headers) {
            if (response.c == "s") {
                delete  response.c;
                fileItem.webUrl=response.fileVO.webUrl;
                $scope.list.push({file: response.fileVO, audio: response.audioInfoVO});
            } else {
                fileItem.isSuccess = 'uploadFail';
            }
        },
        url: api + "/cp/programs/audio",
        headers: {'X-Auth-Token': $scope.userInfo.token}
    })

    $scope.uploadAllAudio = function () {
        $scope.uploaderAudio.uploadAll();
    }

    $scope.cancel=function () {
        $uibModalInstance.close("cancel");

    }


    $scope.ok = function () {
        if($scope.list.length!=$scope.uploaderAudio.queue.length){
            $scope.message="请等待上传完成"
            return false;
        }
        $scope.disabled=true;

        AlbumManager.albumQuickAdd(albumId,$scope.list).then(function (result) {
            $scope.disabled=false;

            if(result.data.c=="s"){
                $uibModalInstance.close("ok");
            }else{
                $uibModalInstance.close("error");
            }
        })
    }

}])

